/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Fly.Data.MySql;
import Fly.Data.Result;
import java.util.ArrayList;

/**
 *
 * @author griever
 */
public class Base extends MySql {
    
    public String buildWhere(String fields)  {
        String fieldArr[] = fields.split(",");
        String where = "";
        //FIXME: Should we use reflection?
        for(int i=0;i<fieldArr.length;i++) {
            String field = fieldArr[i];
            where += field + " = ?" + field;
            if (i == fieldArr.length-2) {
                where += " AND ";
            }
        }
        return where.trim();
    }
    
    public ArrayList find(String fields)    {
        return this.find(fields, "");
    }
    
    public ArrayList find(String fields, String choice)    {
        String where = this.buildWhere(fields);
        Object result = this.Select(where, choice, "", "").Data;
        if (result instanceof ArrayList)
            return (ArrayList) result;
        return null;
    }
    
    public boolean isExist(String fields)    {
        ArrayList data = this.find(fields);
        if (data == null)   {
            return false;
        }
        if (data.isEmpty())
            return false;
        return true;
    }
}
